package com.xiaoge.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoge.auth.annotation.ControllerEndpoint;
import com.xiaoge.response.Result;
import com.xiaoge.system.entity.File;
import com.xiaoge.system.service.FileService;
import com.xiaoge.system.vo.FileVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiaoge
 * @since 2021-02-17
 */
@RestController
@RequestMapping("/file")
//允许跨域
@CrossOrigin
public class FileController {

    private FileService fileService;

    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    @ApiOperation(value = "分页查询文件")
    @PostMapping("/findFilePage")
    public Result findFilePage(@RequestParam(defaultValue = "1") Integer current,
                               @RequestParam(defaultValue = "6") Integer size,
                               @RequestBody FileVo fileVo) {
        //取得分页条件
        Page<File> page = new Page<>(current, size);
        //获得wragger
        QueryWrapper<File> queryWrapper = getWrapper(fileVo);

        //执行查询
        IPage<File> filePage = fileService.page(page, queryWrapper);

        long total = filePage.getTotal();
        List<File> records = filePage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    /**
     * 添加文件记录
     *
     * @param file
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加文件记录失败", operation = "添加文件记录")
    @ApiOperation(value = "添加文件")
    @PostMapping("/addFile")
    public Result addFile(@RequestBody File file) {
        try {
            fileService.save(file);
            return Result.ok();
        } catch (Error e) {
            return Result.error();
        }
    }

    /**
     * 根据id删除单个文件
     *
     * @param id
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除文件记录失败", operation = "删除文件记录")
    @ApiOperation(value = "删除文件")
    @PostMapping("/deleteFileById")
    public Result deleteFileById(Long id) {
        try {
            fileService.removeById(id);
            return Result.ok();
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 根据id批量删除用户
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @PostMapping("/deleteFilesByIds")
    public Result deleteFilesByIds(String ids) {
        String[] idList = ids.split(",");
        fileService.removeByIds(Arrays.asList(idList));
        return Result.ok().message("批量删除成功");
    }

    /**
     * 根据url删除文件
     *
     * @param fileUrl
     * @return
     */
    @ApiOperation(value = "删除文件")
    @ControllerEndpoint(exceptionMessage = "删除文件记录失败", operation = "删除文件记录")
    @PostMapping("/deleteFileByUrl")
    public Result deleteFileByUrl(String fileUrl) {
        QueryWrapper<File> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(fileUrl)) {
            queryWrapper.eq("url", fileUrl);
        }
        try {
            fileService.remove(queryWrapper);
            return Result.ok();
        } catch (Exception e) {
            return Result.error();
        }
    }


    /**
     * 根据url批量删除文件
     *
     * @param fileUrls
     * @return
     */
    @ApiOperation(value = "批量删除文件")
    @PostMapping("/deleteFilesByUrls")
    public Result deleteFilesByUrls(String fileUrls) {
        String[] urlList = fileUrls.split(",");
        if (urlList.length > 0) {
            for (String s : urlList) {
                QueryWrapper<File> queryWrapper = new QueryWrapper<>();
                if (s != null) {
                    queryWrapper.eq("url", s);
                }
                try {
                    fileService.remove(queryWrapper);
                } catch (Exception e) {
                    return Result.error();
                }
            }
        }
        return Result.ok();
    }

    private QueryWrapper<File> getWrapper(FileVo fileVo) {
        QueryWrapper<File> queryWrapper = new QueryWrapper<>();
        if (fileVo != null) {
            //查询每个条件
            if (!ObjectUtils.isEmpty(fileVo.getFileName())) {
                queryWrapper.like("file_name", fileVo.getFileName());
            }
            if (!ObjectUtils.isEmpty(fileVo.getType())) {
                queryWrapper.like("type", fileVo.getType());
            }
            //模糊查询
            if (!ObjectUtils.isEmpty(fileVo.getUrl())) {
                queryWrapper.like("url", fileVo.getUrl());
            }
        }
        return queryWrapper;
    }
}
